﻿/*
==============================================================================
// 角色管理 页面控制层代码。
==============================================================================
*/
(function ($) {

    if (!window.platform) { window.platform = new Object(); }
    if (!window.platform.RoleManage) { window.platform.RoleManage = new Object(); }

    window.platform.RoleManage.initPage = function (createFunction, editFunction, removeFunction) {

        var _bindQueryButtonEvent = function () {
            $("#aquery", "#queryFm").bind("click", function () {
                var fm = $('#queryFm').serialize();
                $('#dg').datagrid('load', { model: fm });
            });
        };

        var _bindToolBarButtonEvent = function () {
            $("#acreate", "#toolbar").bind("click", function () {
                _createRole();
            });
            $("#aedit", "#toolbar").bind("click", function () {
                _editRole();
            });
            $("#aremove", "#toolbar").bind("click", function () {
                _batchAction("Remove");
            });

            $("#areload", "#toolbar").bind("click", function () {
                _reloadRole();
            });

        };

        var _RoleDataGridInit = function () {
            var options = {
                singleSelect: true,
                checkOnSelect: false,
                selectOnCheck: false,
                fit: true,
                border: false,
                rownumbers: true,
                nowrap: true,
                striped: true,
                remoteSort: false,
                url: "/Role/RoleJson",
                queryParams: { model: $('#queryFm').serialize() },
                idField: "ID",
                frozenColumns: [[
                      { field: 'ID', checkbox: true },
                      { field: 'Name', title: '角色名称', width: 250, align: 'center' },

                ]],
                columns: [[
                   {
                       field: 'Code', title: '角色编号', width: 80, align: 'center',

                   },


                   { field: 'Remark', title: '备注', width: 250, align: 'center' }

                ]],
                pagination: true,

                toolbar: "#toolbar",
                rowStyler: function (index, row) {
                    //根据状态值设置该行的样式
                    if (row.BeLock) {
                        return 'color:#ff0000;font-weight:bold;';
                    }
                    else {
                        return '';
                    }

                },
                onDblClickRow: function (rowIndex, rowData) {
                    if (editFunction) {
                        _editRole(rowData);
                    }
                },
                onLoadSuccess: function (data) {
                    _resetBtnState();
                },
                onCheck: function (rowIndex, rowData) {
                    _resetBtnState();
                },
                onUncheck: function (rowIndex, rowData) {
                    _resetBtnState();
                },
                onCheckAll: function (rows) {
                    _resetBtnState();
                },
                onUncheckAll: function (rows) {
                    _resetBtnState();
                }
            };

            $("#dg").datagrid(options);
        };

        _bindQueryButtonEvent();
        _bindToolBarButtonEvent();
        _RoleDataGridInit();
    };

    _resetBtnState = function () {
        var rows = $('#dg').datagrid('getChecked');
        if (rows.length != 0) {
            if (rows.length > 1) {
                //若选中了多条，禁用编辑按钮
                $('#aedit').linkbutton('disable');
               
            }
            else {
                //若选中仅一条，开放编辑按钮
                $('#aedit').linkbutton('enable');
            }
            $('#aremove').linkbutton('enable');
            $('#areset').linkbutton('enable');
        }
        else {
            //全不选，禁用按钮
            $('#aedit').linkbutton('disable');
            $('#aremove').linkbutton('disable');
            $('#areset').linkbutton('disable');
           
        }
    };

    _createRole = function () {
        $("<div/>").attr({ id: "dd" }).dialog({
            title: '新建角色',
            width: 750,
            height: 260,
            closed: false,
            resizable: true,
            cache: false,
            href: '/Role/CreateEdit?id=0',
            modal: true,

            buttons: [{
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    _saveRole($('#dd'));
                }
            },
            {
                text: '关闭',
                iconCls: 'icon-close',
                handler: function () {
                    $('#dd').dialog('close');
                    $('#dd').dialog('destroy');
                }
            }]


        });


    };

    _editRole = function (row) {
        if (row == undefined) {
            row = $('#dg').datagrid('getChecked')[0];
        }
        if (row) {
            $("<div/>").attr({ id: "dd" }).dialog({
                title: '编辑角色',
                width: 750,
                height: 260,
                closed: false,
                resizable: true,
                cache: false,
                href: '/Role/CreateEdit?id=' + row.ID,
                modal: true,

                buttons: [{
                    text: '保存',
                    iconCls: 'icon-save',
                    handler: function () {
                        _saveRole($('#dd'));
                    }
                },
           {
               text: '关闭',
               iconCls: 'icon-close',
               handler: function () {
                   $('#dd').dialog('close');
                   $('#dd').dialog('destroy');
               }
           }]


            });
        }
    };

    _saveRole = function (dialog) {

        var verifyResult = $("form", dialog).form("validate");
        if (!verifyResult) { return false };

        var Role = $(dialog).form().serializeObject();

        var _callback = function (result) {
            if (result.success) {
                //$.messager.alert('操作提醒', '操作成功');
                _reloadRole();
            } else {
                $.messager.alert('错误提醒', result.msg, 'error');
            }
            $('#dd').dialog('close');
            $('#dd').dialog('destroy');
        };

        $.post("/Role/Save", Role, function (result) {
            _callback.call(this, result);
        }, 'json');
    };

    _reloadRole = function () {
        $('#dg').datagrid('clearChecked');
        $('#dg').datagrid('clearSelections');
        $('#dg').datagrid('reload');
    };

    _batchAction = function () {

        var rows = $('#dg').datagrid('getChecked');
        var data = [];
        if (rows.length != 0) {
            for (var i = 0; i < rows.length; i++) {
                data.push(rows[i].ID);
            }
        }
        if (data.length != 0) {
            $.messager.confirm('确认', "您确定要删除所选角色吗", function (r) {
                if (r) {
                    $.post("/Role/BatchDelete", { ids: data.join(',') }, function (result) {
                        if (result.success) {
                            //$.messager.alert('操作提醒', '操作成功');
                            _reloadRole();
                        } else {
                            $.messager.alert('错误提醒', result.msg, 'error');
                        }
                    }, "json");
                }
            });
        }
    };

    _singleAction = function (id) {

        $.messager.confirm('确认', "您确定要删除该角色吗", function (r) {
            if (r) {
                $.post("/Role/SingleDelete", { id: id }, function (result) {
                    if (result.success) {
                        //$.messager.alert('操作提醒', '操作成功');
                        _reloadRole();
                    } else {
                        $.messager.alert('错误提醒', result.msg, 'error');
                    }
                }, "json");
            }
        });
    }


})(jQuery);